import {observable,action,computed} from 'mobx'

class TodoStore {
  // todo列表
  @observable todos=[]
  // 筛选条件
  @observable filter = 'All'
  // 添加任务
  @action.bound todoAdd (taskName) {
    this.todos.push({
      taskName,
      isCompleted: false
    })
  }
  // 删除任务
  @action.bound todoDelete(index){
    this.todos.splice(index,1)
  }
  // 更改任务完成状态
  @action.bound chanageCompleted(index,flag){
    this.todos[index].isCompleted = flag
  }
  // 未完成的任务
  @computed get unfinishedTodoCount(){
    return this.todos.filter(todo=> todo.isCompleted === false).length 
  }
  // 更改筛选条件
  @action.bound changeFilter(condition){
    this.filter = condition
  }

  // 检测筛选条件更改
  @computed get filterTodo () {
    switch (this.filter) {
      case 'All':
        return this.todos
      case 'Active':
        return this.todos.filter(todo=>todo.isCompleted === false)
      case 'Completed':
        return this.todos.filter(todo=>todo.isCompleted === true)
      default:
        return this.todos
    }
  }
}
const todo = new TodoStore()

export default  todo